<template>
  <view>
    <!-- 未选择收货地址时，显示的内容 -->
    <view class="address-choose-box" v-if="Object.keys(address).length === 0">
      <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
    </view>

    <!-- 选择收货地址后，显示的内容 -->
    <view class="address-info-box" v-else>
      <view class="row1">
        <view class="row1-left">
          <text style="font-weight:bold">收货人：</text>
          <text>{{address.userName}}</text>
        </view>
        <view class="row1-right">
          <text style="font-weight:bold">电话：</text>
          <text>{{address.telNumber}}</text>
          <uni-icons type="arrowright" size="16" @click="chooseAddress" />
        </view>
      </view>
      <view class="row2">
        <text style="font-weight:bold" class="row2-left">收货地址：</text>
        <text class="row2-right">{{addrstr}}</text>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    name: "my-address",
    data() {
      return {
        // 收货地址
        // address: {}  // 改为用 vuex store user.js中的存储
      };
    },
    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      // 点击获取微信用户收货地址事件
      async chooseAddress() {
        const [err, succ] = await uni.chooseAddress().catch(fail => fail)
        if (err === null && succ.errMsg === 'chooseAddress:ok') {
          this.updateAddress(succ)
        }
        // 如果用户点击了拒绝授权按钮，则不会再弹出选择地址，需要重新弹出是否允许授权提示
        // chooseAddress:fail auth deny为安卓端提示，chooseAddress:fail authorize no response 为ios端提示
        if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg ===
            'chooseAddress:fail authorize no response')) {
          this.reAuth() // 调用自定义弹出重新授权提示的方法
        }
      },
      // 用户拒绝授权地址后，弹出重新授权提示的方法
      async reAuth() {
        // 提示用户对地址进行授权
        const [err, confirmResult] = await uni.showModal({
          content: '检测到您没打开地址权限，是否去设置打开？',
          confirmText: '是',
          cancelText: '否'
        })
        // 如果弹框异常，则直接退出
        if (err) return
        // 如果用户点击了 “取消” 按钮，则提示用户 “您取消了地址授权！”
        if (confirmResult.cancel) return uni.$showMsg('您取消了地址授权！')

        // 如果用户点击了 “确认” 按钮，则调用 uni.openSetting() 方法进入授权页面，让用户重新进行授权
        if (confirmResult.confirm) return uni.openSetting({
          // 授权结束，需要对授权的结果做进一步判断
          success: (settingResult) => {
            // 地址授权的值等于 true，提示用户 “授权成功”
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            // 地址授权的值等于 false，提示用户 “您取消了地址授权”
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          }
        })
      }
    },
    computed: {
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['addrstr'])
    }
  }
</script>

<style lang="scss">
  .address-choose-box {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .address-info-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    font-size: 12px;
    padding: 0 5px;
    height: 90px;
    box-sizing: border-box;

    .row1 {
      display: flex;
      justify-content: space-between;
    }

    .row2 {
      margin-top: 10px;
      display: flex;

      .row2-left {
        white-space: nowrap;
      }
    }
  }
</style>
